package com.thfw.test.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by zhaoboliang on 16/6/16.
 */
public class ConfigurationService {
    private static final String TAG = ConfigurationService.class.getCanonicalName();

    Context mContext;
    SharedPreferences mSharedPreferences;
    SharedPreferences.Editor mEditor;
    public ConfigurationService(Context context){
        mContext = context;
        mSharedPreferences = mContext.getSharedPreferences(TAG,Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
    }

    public void putInt(String key,int value){
        putInt(key,value,false);
    }
    public void putInt(String key,int value,boolean commit) {
        mEditor.putInt(key,value);
        commit(commit);
    }

    public void putLong(String key,long value){
        putLong(key, value, false);
    }
    public void putLong(String key,long value,boolean commit) {
        mEditor.putLong(key, value);
        commit(commit);
    }

    public void putBoolean(String key,boolean value){
        putBoolean(key, value, false);
    }
    public void putBoolean(String key,boolean value,boolean commit) {
        mEditor.putBoolean(key, value);
        commit(commit);
    }

    public void putFloat(String key,float value){
        putFloat(key, value, false);
    }
    public void putFloat(String key,float value,boolean commit) {
        mEditor.putFloat(key, value);
        commit(commit);
    }

    public void putString(String key,String value){
        putString(key, value, false);
    }
    public void putString(String key,String value,boolean commit) {
        mEditor.putString(key, value);
        commit(commit);
    }

    public int getInt(String key,int defaultValue){
        int value;
        value = mSharedPreferences.getInt(key,defaultValue);
        return value;
    }

    public float getFloat(String key,float defaultValue){
        float value;
        value = mSharedPreferences.getFloat(key, defaultValue);
        return value;
    }

    public long getlong(String key,long defaultValue){
        long value;
        value = mSharedPreferences.getLong(key, defaultValue);
        return value;
    }

    public boolean getBoolean(String key,boolean defaultValue){
        boolean value;
        value = mSharedPreferences.getBoolean(key, defaultValue);
        return value;
    }
    public String getString(String key,String defaultValue){
        String value;
        value = mSharedPreferences.getString(key, defaultValue);
        return value;
    }

    public boolean contains(String key) {
        return mSharedPreferences.contains(key);
    }

    private void commit(boolean value){
        if(value) {
            mEditor.commit();
        }
    }

    public boolean commit(){
        return mEditor.commit();
    }
}
